// login/phone/phone.js
let App = getApp();
var that;
Page({

  /**
  * 页面的初始数据
  */
  data: {
    tel: "",
    code: "",
    newPassword: "",
    sendTime: '发送验证码',
    sendColor: '#363636',
    snsMsgWait: 60,
    open_id: '',
    userInfo: ''
  },
  inputTel(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  inputCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取验证码
  sendCode: function () {
    var that = this;
    if (that.data.tel == "") {
      wx.showModal({
        title: '请输入手机号',
        showCancel: false
      });

      return;
    }

    if (!(/^1[3|4|5|8|7|9][0-9]\d{8}$/.test(that.data.tel))) {
      console.log(that.data.tel)
      wx.showModal({
        title: '手机号输入错误',
        showCancel: false
      });
      return;
    }

    // 60秒后重新获取验证码
    var inter = setInterval(function () {
      that.setData({
        smsFlag: true,
        sendColor: '#cccccc',
        sendTime: that.data.snsMsgWait + 's后重发',
        snsMsgWait: that.data.snsMsgWait - 1
      });
      if (that.data.snsMsgWait < 0) {
        clearInterval(inter)
        that.setData({
          sendColor: '#363636',
          sendTime: '发送验证码',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(that), 1000);

    // 写自己的服务器和接口- - 
    wx.request({
      url: App.url.requestUrl + 'index/send_msg',
      method: 'POST',
      header: App.header,
      data: {
        phone: that.data.tel,
      },
      success(res) {
        console.log(res);
        if (res.data.ret == 200) {
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  login() {
    that = this;
    wx.request({
      url: App.url.requestUrl + 'index/codeLogin',
      method: 'POST',
      header: { 'content-type': 'application/json' },
      dataType: 'json',
      data: {
        phone: that.data.tel,
        code: that.data.code,
        open_id: wx.getStorageSync('openid')
      },
      success: function (res) {
        if (res.data.ret == 200) {
          wx.setStorageSync('access_token', res.data.data.access_token);
         
            wx.showToast({
              title: '登陆成功',
              icon: 'success',
              duration: 2000
            })

            // 跳转首页
            setTimeout(function () {
              wx.switchTab({
                url: '../../shop/index/index',
              })
            }, 2500)

         
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getopenid() {
    wx.login({
      success: function (res) {
        console.log(res)
        if (res.code) {
          console.log('通过login接口的code换取openid')
          wx.request({
            url: App.url.requestUrl + 'user/checkUserReg',

            data: {
              code: res.code
            },
            method: 'post',
            header: { 'content-type': 'application/json' },
            success: function (openIdRes) {
              that.userinfo(openIdRes.data.data.open_id)

              console.log(openIdRes.data)
              console.info("登录成功返回的openId：" + openIdRes.data.data.open_id);

            },
            fail: function (error) {
              console.info("获取用户openId失败");
              console.info(error);
            }
          })
        }
      }
    })
  },
  userinfo(open_id) {
    wx.request({
      url: App.url.requestUrl + 'user/editUser',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      data: {
        open_id: open_id,
        mobile: that.data.tel,
        nickname: that.data.userInfo.nickName, 
        gender: that.data.userInfo.gender,
        avatar: that.data.userInfo.avatarUrl

      },
      success: function (lres) {
        if (lres.data.ret == 200) {
          wx.showToast({
            title: '登陆成功',
            icon: 'success',
            duration: 2000
          })

          // 跳转首页
          setTimeout(function () {
            wx.switchTab({
              url: '../../shop/index/index',
            })
          }, 2500)
        }


      }
    })
  },

  onGotUserInfo(e) {

    console.log("nickname=" + e.detail.userInfo.nickName);
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    if (options.tel){
      this.setData({
        tel: options.tel

      })
    }
    wx.getUserInfo({
      success: res => {
        console.log(res.userInfo)    //获取的用户信息还有很多，都在res中，看打印结果
        this.setData({
          userInfo: res.userInfo

        })
      }
    })
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  }
})
